import 'dart:convert';
import "dart:collection";
import 'package:flutter/services.dart';
import 'package:crypto/crypto.dart';
import 'package:http/http.dart' as http;
import 'package:archime/model/robot_message.dart';
//import 'package:archime/event/event_robot_respone.dart';
import 'package:archime/event/event_server_respone.dart';
import 'package:archime/event/event_login_respone.dart';

class MimcUtil{
  static const String CHANNEL = "com.ithaca.archime/mimc";
  static const MethodChannel methodChannel = MethodChannel(CHANNEL);
  static const BasicMessageChannel<dynamic> rcvSender = const BasicMessageChannel(CHANNEL,StandardMessageCodec());

  // mimc返回消息处理初始化
  init(){
    rcvSender.setMessageHandler((param){
      operateNativeMessage(param);
      return;
    });
  }

  // 注册
  Future<Null> sendRegisterMsg(uid, password) async {
    var map = new Map();
    map["account"] = "AdminRegister0829";
    map["uid"] = uid;
    map["password"] = password;
    print("点击了登录 --> 发消息给Android");
    String result = await methodChannel.invokeMethod('sendRegisterMsg', map);
    print(result);
  }

  // 登录
  Future<Null> sendLoginMsg(uid, password) async {
    var map = new Map();
    map["account"] = "AdminLogin0829";
    map["uid"] = uid;
    map["password"] = password;
    String result = await methodChannel.invokeMethod('sendLoginMsg', map);
    print(result);
  }

  Future<Null> sendSingleMessage() async {
    Map<String, String> map = { "account": "AdminUser0829" };
    String result = await methodChannel.invokeMethod('sendSingleMsg', map);
    print(result);
  }

  // mimc 返回消息处理
  operateNativeMessage(param){
    print("param:"+param);
    if(param is String){
      Map<String, dynamic> msg = json.decode(param);

      if(msg['code'] == 201){
        eventBusLogin.fire(EventLoginRespone(201,param));
      }else if(msg['code'] == 409){
        eventBusLogin.fire(EventLoginRespone(409,param));
      }else if(msg['code'] == 202){
        eventBusLogin.fire(EventLoginRespone(202,param));
      }else if(msg['code'] == 410){
        eventBusLogin.fire(EventLoginRespone(410,param));
      }else{
//        eventBus.fire(EventRobotRespone(param));
      }
    }
  }

}